layui.use(['table', 'laytpl', 'layer'], function() {
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laytpl = layui.laytpl;
	var flowername;
	$("#searchBtn").click(function() {
		flowername = $("#flowername").val();
		getannounce();
	})
	$("#reLoad").on("click", function() {
		flowername = "";
		layer.msg("刷新", {
			time: 500
		}, function() {
			getannounce();
		});
	})
	getannounce();

	function getannounce() {
		$.ajax({
			type: "post",
			url: "/getflower",
			data: {
				flowername: flowername
			},
			async: true,
			success: function(data) {
				var result = $.parseJSON(data);
				var tablelist = table.render({
					elem: '#test',
					height: 'full-95',
					page: true, //开启分页,
					limits: [10],
					limit: 10,
					id: 'table_reload',
					cols: [
						[ //表头
							{
								checkbox: true,
								fixed: true
							},
							{
								field: 'flowername',
								title: '鲜花名称',
								width: 150,
							}, {
								field: 'flowerprice',
								title: '鲜花价格',
								width: 150,
								sort: true
							}, {
								field: 'love',
								title: '喜欢人数',
								width: 130,
								sort: true
							}, {
								field: 'sold',
								title: '售出数量',
								width: 130,
								sort: true
							}, {
								field: 'src',
								title: '存储目录',
								width: 170
							}, {
								field: 'type',
								title: '类型',
								width: 100
							}, {
								fixed: 'right',
								width: 300,
								title: '操作',
								align: 'center',
								toolbar: '#barDemo'
							}
						]
					],
					"data": result
				});
			}
		});
	}
	table.on('tool(lay_table_filter)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		var data = obj.data //获得当前行数据,
		layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'edit') {
			detail(data.id, "edit");
		} else if(layEvent === 'detail') {
			checkDetails(data.id, "detail");
		}
	});
	//编辑鲜花信息
	function detail(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '编辑鲜花信息',
			type: 2,
			content: '/static/msg/flower/flower_edit.html?id=' + id + "&type=" + type,
			area: ['100%', '100%'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				var body = layer.getChildFrame('body', index);
				var img = body.find("#flowerimg")[0].src;
				var flowername = body.find(".flowername").val();
				var flowerprice = body.find(".flowerprice").val();
				var love = body.find(".love").val();
				var sold = body.find(".sold").val();
				var type = body.find(".type").val();
				var isgo = true;
				if(!flowername || flowername == undefined || flowername == "") {
					layer.msg('鲜花名字不能为空', {
						time: 2000
					});
					var isgo = false;
				}
				if(!flowerprice || flowerprice == "" || flowerprice == undefined) {
					layer.msg('鲜花价格不能为空', {
						time: 2000
					});
					var isgo = false;
				};
				if(isgo) {
                    tijiao();
				}

				function tijiao() {
					$.ajax({
						type: "post",
						url: "/flowershow/edit",
						data: {
							id: id,
							flowername: flowername,
							flowerprice: flowerprice,
							love: love,
							sold: sold,
							img: img
						},
						async: true,
						success: function(data) {
							layer.msg(data.massage, {
								time: 1000
							}, function() {
								if(data.code == '1') {
									layer.close(index);
									getannounce();
								}
							});
						}
					});
				}
			}
		});
	}
	//查看鲜花信息
	function checkDetails(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '鲜花详情',
			type: 2,
			content: '/static/msg/flower/flower_edit.html?id=' + id + "&type=" + type,
			area: ['100%', '100%'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				layer.close(index);
			}
		});
	}
});